﻿@model FollowPeers.Models.PublicationModel
@using FollowPeers.Models;
@{
    ViewBag.Title = "Publication Details";
}
@Html.Partial("_Header")
 @{
     FollowPeers.Models.FollowPeersDBEntities followPeersDB = new FollowPeers.Models.FollowPeersDBEntities();
     string name = Membership.GetUser().UserName;
     ViewBag.UserName = name;
     FollowPeers.Models.UserProfile myprofile = followPeersDB.UserProfiles.First(p => p.UserName == name);
     AchievementLike achmodel = followPeersDB.AchievementLikes.SingleOrDefault(p => p.AchievementId == Model.publicationID && p.UserProfileId == myprofile.UserProfileId && p.Type == 6);
  
}
<div class="container">
    <div class="row">
        @Html.Partial("_SideBar1")
        @Script("jquery.validate.min.js")
        @Script("jquery.validate.unobtrusive.min.js")
        @Script("jquery.bgiframe.js")
        @Script("jquery.mcdropdown.js")
        <link rel="stylesheet" type="text/css" href="../../Content/css/jquery.mcdropdown.css" />
        <link href="../../Content/css/jquery-ui.css" rel="stylesheet" type="text/css" />
        <div class="span7">
           <h3> @Html.DisplayFor(model => model.title)</h3>
           <div class="display-field">By @Html.DisplayFor(model => model.author)</div>
           <hr />
           <div style="height:300px">
           <ul class="nav nav-tabs">               
                    <li id="f_ftab1" class="active" onclick="ftab('ftab1')"><a>Abstract</a></li>
                    <li id="f_ftab2" onclick="ftab('ftab2')"><a>Reader</a></li>
                    <li id="f_ftab3" onclick="ftab('ftab3')"><a>Bibliographic Information</a></li>
            </ul>
           <div id="ftab1" style="display: none;" class="tab-pane active" >
            <br />
                @Html.DisplayFor(model => model.description)
           </div>
           <div id="ftab2" style="display: none;" class="tab-pane" >
           @if (Model.filename != null && Model.filename != "")
           {
                <embed src="\\Content\\Files\\@Model.UserProfile.UserName\\Publications\\@Model.filename" />
           }
           else
           { 
               <div>No pdf/doc version of this publication available.</div>
           }
           </div>
           <div id="ftab3" style="display: none;" class="tab-pane" >
            <table>
            <tr>
                <td style="font-weight:bold">Type:</td>
                <td>   @Html.DisplayFor(model => model.type)</td>
            </tr>
            <tr>
                <td style="font-weight:bold">Year:</td>
                <td>   @Html.DisplayFor(model => model.year)</td>
            </tr>
            <tr>
                <td style="font-weight:bold">Status:</td>
                <td>   @Html.DisplayFor(model => model.status)</td>
            </tr>
            </table>
            @{
                if (Model.journal != "default")
                { 
                    <div class="display-label">
                        Journal:</div>
                    <div class="display-field">
                        @Html.DisplayFor(model => model.journal)
                    </div>
                }
            }
            @{
                if (Model.publisher != "default")
                { 
                    <div class="display-label">
                        Publisher:</div>
                    <div class="display-field">
                        @Html.DisplayFor(model => model.publisher)
                    </div>
                }
            }
            @{
                if (Model.conference != "default")
                {
                        <div class="display-label">
                        Conference:</div>
                    <div class="display-field">
                        @Html.DisplayFor(model => model.conference)
                    </div>
                }
            }
            @{
                if (Model.university != "default")
                {
                <div class="display-label">
                University:</div>
            <div class="display-field">
                @Html.DisplayFor(model => model.university)
            </div>
                }
            }
            @{
                if (Model.referenceID != "")
                {
                <div class="display-label">
                Location:</div>
            <div class="display-field">
                @Html.DisplayFor(model => model.referenceID)
            </div>
                }
            }
            @{
                if (Model.issue != "default")
                {
                    <div class="display-label">
                Issue: @Html.DisplayFor(model => model.issue)
            </div>
                }
            }
            
            @{
                if (Model.volume != "default")
                {
                    <div class="display-label">
                Volume:</div>
            <div class="display-field">
                @Html.DisplayFor(model => model.volume)
            </div>
                }
            }
            
            @{
                if (Model.page != "default")
                {
                <div class="display-label">
                Page:</div>
            <div class="display-field">
                @Html.DisplayFor(model => model.page)
            </div>
                }
            }
            </div>
           </div>
                <input id="publicationIdAJAX" style="display:none" type="text" value="@Model.publicationID" >
        <ul class="nav nav-tabs" id="myTab">               
                    <li id="li_tab1" class="active" onclick="tab('tab1')"><a>Comments</a></li>
                    <li id="li_tab2" onclick="tab('tab2')"><a>Discussions</a></li>
        </ul>
        <div class="tab-content">
        <div id="tab1" style="display: none;" class="tab-pane active" >
        @{
            Html.RenderAction("Index", "Comment", new { ParentType = "PublicationModel", ParentId = Model.publicationID });
            <p>
                @using (Html.BeginForm("Create", "Comment", FormMethod.Post, new { ParentType = "PublicationModel", ParentId = Model.publicationID, SubmittedBy = Membership.GetUser().UserName, TimeSent = DateTime.Now, Flagged = false, Starred = false }))
                {
                    <fieldset>
                        <input type="hidden" name="ParentType" value="PublicationModel" />
                        <input type="hidden" name="ParentId" value="@Model.publicationID" />
                        <input type="hidden" name="SubmittedBy" value="@ViewBag.UserName" />

                        <div class="editor-label">
                            <label>Add Comment</label>
                            @*@Html.LabelFor(model => model.CommentBody, " Add Comment:")*@
                        </div>
                        <div class="editor-field">
                            <textarea name="CommentBody" cols="100" rows="2"></textarea>
                           @* @Html.TextAreaFor(model => model)
                            @Html.ValidationMessageFor(model => model.CommentBody)*@
                        </div>
                        <input type="submit" value="Submit" />
                    </fieldset>
                }              
            </p>
        }
        </div>
        <div id="tab2" style="display: none;" class="tab-pane" >
        @if (Model.ownerID == myprofile.UserProfileId)
        {
            <button id="Topic" class="btn btn-primary">Add Discussion</button>
            foreach (var forums in followPeersDB.Forums)
            {
                //var currentForumTopic = followPeersDB.ForumTopics.SingleOrDefault(t => t. == currentuser);
                List<ForumTopic> topics = forums.Topics.ToList();
                foreach (var topic in topics)
                {
                    //if ((((group != null && group.Members.Contains(currentuser) && group2 != null && !group2.Members.Contains(currentuser)) || (group != null && !group.Members.Contains(currentuser) && group2 != null && group2.Members.Contains(currentuser))) && topic.createdUser != null && topic.createdUser != currentuser) || (topic.createdUser != null && topic.createdUser == currentuser && topic.groupId == Model.GroupId.ToString()))
                    if (topic.groupId == Model.publicationID.ToString())
                    {
                                                
                                                <tr >
                                                    <td>
                                                        <div class="MessageContainer">
                                                            <div class="messageDetails" style="width:73%;float:left;">
                                                                <div class="messageName"><h4><a href="/Forum/TopicDetail/?id=@topic.ForumTopicId">@topic.Name</a></h4></div>
                                                                <div class="messageSubject">@topic.Description</div>
                                                                <div class="messageName"><a>Posts: </a>@topic.Posts.Count</div>                                                                
                                                            </div>    
                                                        </div>
                                                    </td>
                                                </tr>
                    }
                }
            }
            
            
        }
        </div>
        </div>
                    </div>  
        <div class="span3">
         <div class="scrollspy" data-spy="scroll" data-target=".navlist" style="margin-left:5px">
            
            @{
                if (myprofile.UserProfileId == Model.ownerID)
                {
                    @Html.ActionLink("Edit", "Edit", new { id = Model.publicationID }) <text>|</text>
                    @Html.ActionLink("Delete", "Delete", new { id = Model.publicationID })
                    <br /><br />
                    <text>Recommend Publication:</text>
                    <div class="editor-field" id ="RecommendList">
                        <input type="text" id="Recommend" name="Recommend" style="width:150px" />&nbsp;<a id="Click1" style="height:50px;" class="icon-hand-right"></a>
                    </div>         
                    <text>View Count: @Model.viewCount </text> 
                             <br />                                     
                    @Html.ActionLink("Back to Publication Db", "Index");

                }
                else
                {   
                    <br />
                    <table><tr>
                    @Html.ActionLink("Mark As Favourite", "FavouritePub", new { id = Model.publicationID, Pubname = Model.title })
                    @if (@Model.Likes != 0)
                    {
                       <td>
                    <span class="label label-default">@Model.Likes 
                                                        <img src="../../Content/images/icons/16x16/thumb_up.png" />
                                                    </span></td>
                    }
                    else
                    {
                    <td>
                        <span class="label label-default">0 
                                                        <img src="../../Content/images/icons/16x16/thumb_up.png" />
                                                    </span></td>
                    }
                    @if (@achmodel == null)
                    {
                        <td>
                                                    <div class="btn-group">
                                                        <button class="btn btn-mini" style="width: 50px">
                                                                <img id="Likepub" src="../../Content/images/icons/16x16/thumb_up.png" />                         
                                                        </button>
                                                    </div></td>
                    }
                    else
                    {
                    <td>
                        <input type="hidden" id="achid" value="@achmodel.AchievementlikeId" />
                                                    <div class="btn-group">
                                                        <button class="btn btn-mini" style="width: 50px">
                                                            
                                                                <img id="Unlikepub" src="../../Content/images/icons/16x16/thumb_down.png" />
                                                               
                                                        </button>
                                                    </div></td>
                    }
                    </tr></table>
                    <br />
                    <br />
                    <text>Recommend Publication: </text> 
                    <div class="editor-field" id="RecommendList2">
                    <input type="text" id="Recommend2" name="Recommend2" style="width:150px" />&nbsp;<a id="Click2" style="height:50px;"class="icon-hand-right"></a>
                    </div> 
                    List<int> ranks = new List<int>();

                    for (int i = 1; i <= 5; i++)
                    {
                        ranks.Add(i);
                    }
                    
                  <text>Select Rank: </text> @Html.DropDownList("Ranks", new SelectList(ranks), new { style = "width:45px" })
                    
                <br />
                  
                    if (@Model.filename != null)
                    {
                        string path = HttpRuntime.AppDomainAppPath;
                        string newpath = path + "\\Content\\Files\\";
                        var directoryInfo = new DirectoryInfo(newpath + Model.UserProfile.UserName + "\\Publications\\");
                        if (directoryInfo.Exists)
                        {
                             string downloadPath = newpath + Model.UserProfile.UserName + "\\Publications\\" + @Model.filename;
                    <button class="btn btn-success">
                <i class="icon-download"></i> @Html.ActionLink("Download", "Download", new { downloadPath = newpath + Model.UserProfile.UserName + "\\" + Model.filename}) </button> 
                        }
                    }
                <br /><br />
                    @Html.ActionLink("Back to Publication Db", "Index");
                }
                }
   </div>
    <hr />
       @Html.Partial("_PubModelSideBar");
        </div>
        </div>
</div>
@Html.Partial("_Footer")
@helper Script(string scriptName)
    {
    <script src="@Url.Content("~/Scripts/" + scriptName)" type="text/javascript"></script>
}

<script type="text/javascript" language="javascript">
    $(document).ready(function () {
        tab('tab1');
        ftab('ftab1');
        if ($('#RecommendList').length) {
            AutoComplete1();
        }
        function AutoComplete1() {
            $("#Recommend").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        type: "POST",
                        url: "/PublicationModel/RecommendedNames",
                        data: { searchText: extractLast(request.term), maxResults: 10 },
                        dataType: "json",
                        success: function (data) {
                            response($.map(data, function (item) {
                                return { label: item.name, value: item.name, id: item.value }
                            }))
                        },
                        error: function (result) {
                            alert("Error");
                        }
                    });
                },
                focus: function () {
                    // prevent value inserted on focus
                    return false;
                },
                select: function (event, ui) {
                    var terms = split(this.value);
                    // remove the current input
                    terms.pop();
                    // add the selected item
                    terms.push(ui.item.value);
                    // add placeholder to get the comma-and-space at the end
                    terms.push("");
                    this.value = terms.join(", ");
                    $('#RecommendList').append("<input type='hidden' id='Invites" + ui.item.id + "' name='InvitesId'  />");
                    $('#Recommend' + ui.item.id).val(ui.item.id);
                    return false;
                }
            });
            $("#Recommend").bind("keydown", function (event) {
                if (event.keyCode === $.ui.keyCode.TAB &&
                $(this).data("autocomplete").menu.active) {
                    event.preventDefault();
                }
            })
            function split(val) {
                return val.split(/,\s*/);
            }
            function extractLast(term) {
                return split(term).pop();
            }

        }

        if ($('#RecommendList2').length) {
            AutoComplete2();
        }
        function AutoComplete2() {
            $("#Recommend2").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        type: "POST",
                        url: "/PublicationModel/RecommendedNames",
                        data: { searchText: extractLast(request.term), maxResults: 10 },
                        dataType: "json",
                        success: function (data) {
                            response($.map(data, function (item) {
                                return { label: item.name, value: item.name, id: item.value }
                            }))
                        },
                        error: function (result) {
                            alert("Error");
                        }
                    });
                },
                focus: function () {
                    // prevent value inserted on focus
                    return false;
                },
                select: function (event, ui) {
                    var terms = split(this.value);
                    // remove the current input
                    terms.pop();
                    // add the selected item
                    terms.push(ui.item.value);
                    // add placeholder to get the comma-and-space at the end
                    terms.push("");
                    this.value = terms.join(", ");
                    $('#RecommendList2').append("<input type='hidden' id='Invites2" + ui.item.id + "' name='InvitesId2'  />");
                    $('#Recommend2' + ui.item.id).val(ui.item.id);
                    return false;
                }
            });
            $("#Recommend2").bind("keydown", function (event) {
                if (event.keyCode === $.ui.keyCode.TAB &&
                $(this).data("autocomplete").menu.active) {
                    event.preventDefault();
                }
            })
            function split(val) {
                return val.split(/,\s*/);
            }
            function extractLast(term) {
                return split(term).pop();
            }

        }
    });



    $("#bookmark_it").click(function () {
        // Get publication ID
        var id1 = $("#publicationIdAJAX").val();
        var pubId = $("#publicationIdAJAX").val();
        // Send Ajax Request
        $.ajax({
            type: "POST",
            url: "/PublicationModel/AddBookmark",
            data: { id: pubId, RecommendedNames: ID },
            success: function () {
                alert('Bookmarked');
                window.history.back(-1);
            }
        });
    });

    $("#Click2").click(function () {
        var pubId = $("#publicationIdAJAX").val();
        var ID = $("#Recommend2").val();
        $.ajax({
            type: "POST",
            url: "/PublicationModel/Recommend",
            data: { id: pubId, NameId: ID },
            success: function () {
                alert('Recommendation has been sent to user:');
                window.history.back(-1);
            }
        });
    });


    $("#Click1").click(function () {
        var pubId = $("#publicationIdAJAX").val();
        var ID = $("#Recommend").val();
        $.ajax({
            type: "POST",
            url: "/PublicationModel/Recommend",
            data: { id: pubId, NameId: ID },
            success: function () {
                alert('Recommendation has been sent');
                window.history.back(-1);
            }
        });
    });

    $("#Topic").click(function () {
        var pubId = $("#publicationIdAJAX").val();
        window.location = "../AddTopic/" + pubId;
    });


    $("#unbookmark_it").click(function () {
        // Get publication ID
        var id2 = $("#publicationIdAJAX").val();
        // Send Ajax Request
        $.ajax({
            type: "POST",
            url: "/PublicationModel/DeleteBookmark",
            data: "ID=" + id2,
            success: function () {
                alert('Un-Bookmarked');
                window.history.back(-1);
            }
        });
    });

    $("#Likepub").click(function () {
        var id1 = $("#publicationIdAJAX").val();
        var nameId = $("#SubmittedBy").val();
        // Send Ajax Request
        $.ajax({
            type: "POST",
            url: "/PublicationModel/Like",
            data: { id: id1, NameId: nameId },
            success: function () {
                window.location = "../Details/" + id1;
            }
        });
    });

    $("#Ranks").change(function () {
        var id1 = $(this).val();
        var PubId = $("#publicationIdAJAX").val();
        alert(id1);
        $.ajax({
            type: "POST",
            url: "/PublicationModel/Rank",
            data: { id: id1, pubId: PubId },
            success: function () {
                window.location = "../Details/" + PubId;
            }
        });
    });

    $("#Unlikepub").click(function () {
        var id1 = $("#achid").val();
        var pubid1 = $("#publicationIdAJAX").val();
        var nameId = $("#SubmittedBy").val();
        // Send Ajax Request
        $.ajax({
            type: "POST",
            url: "/PublicationModel/Unlike",
            data: { id: id1, pubId: pubid1, NameId: nameId },
            success: function () {
                window.location = "../Details/" + pubid1;
            }
        });
    });


    function tab(tab) {
        document.getElementById('tab1').style.display = 'none';
        document.getElementById('tab2').style.display = 'none';
        document.getElementById('li_tab1').setAttribute("class", "");
        document.getElementById('li_tab2').setAttribute("class", "");
        document.getElementById(tab).style.display = 'block';
        document.getElementById('li_' + tab).setAttribute("class", "active");
    };

    function ftab(ftab) {
        document.getElementById('ftab1').style.display = 'none';
        document.getElementById('ftab3').style.display = 'none';
        document.getElementById('f_ftab3').setAttribute("class", "");
        document.getElementById('f_ftab1').setAttribute("class", "");
        document.getElementById('ftab2').style.display = 'none';
        document.getElementById('f_ftab2').setAttribute("class", "");
        document.getElementById(ftab).style.display = 'block';
        document.getElementById('f_' + ftab).setAttribute("class", "active");
    };


</script>

